﻿@{
    ViewBag.Title = "Product";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@model e.Store.Model.Entities.Product



<div class="col-lg-12">

    @using (Html.BeginForm("AddAndUpdateProduct", "Inventory", FormMethod.Post, new { id = "productForm" }))
    {
        <div id="productCrud" class="panel panel-danger">
            <div class="panel-heading">
                <h3 class="panel-title"><span id="title">Product</span>
                    <span class="pull-right">
                        <a href="/Inventory/Products" title="View All Product" class="btn-sm btn-primary">

                            <span class="btn-label"><i class="icon-table"></i></span>View All Products </a>

                    </span></h3>
            </div>
            <div class="panel-body">

                <div class="col-lg-4 col-lg-offset-1">

                    @Html.HiddenFor(c => c.ProductOID, new { id = "txtProductOID" })

                    <div class="form-group">
                        <label class="control-label" for="txtName">Product Code</label>

                        @Html.TextBoxFor(c => c.ProductCode, new { id = "txtProductCode", @class = "form-control" })

                    </div>
                    <div class="form-group">
                        <label class="control-label" for="txtName">Product Name</label>

                        @Html.TextBoxFor(c => c.ProductName, new { id = "txtProductName", @class = "form-control" })

                    </div>

                    <div class="form-group">
                        <label class="control-label" for="txtName">Select Supplier</label>

                        @Html.DropDownListFor(c => c.SupplierOID, new SelectList(ViewBag.SupplierList, "SupplierOID", "SupplierName"), new { id = "txtSupplierOID", @class = "form-control" })

                    </div>
                    <div class="form-group">
                        <label class="control-label" for="txtName">Select Category</label>

                        @Html.DropDownListFor(c => c.CategoryOID, new SelectList(ViewBag.CategoryList, "CategoryOID", "CategoryName"), new { id = "txtCategoryOID", @class = "form-control" })

                    </div>

                    <div class="form-group">
                        <label class="control-label" for="txtDescription">Description</label>
                        @Html.TextAreaFor(c => c.Description, new { id = "txtDescription", @class = "form-control input-lg" })
                    </div>
                    <div class="form-group">
                        <label class="control-label" for="txtComments">Comments</label>
                        @Html.TextAreaFor(c => c.Comments, new { id = "txtComments", @class = "form-control input-lg" })
                    </div>
                </div>

                <div class="col-lg-4 col-lg-offset-1">

                    <div class="form-group">
                        <label class="control-label" for="txtName">Base Price</label>

                        @Html.TextBoxFor(c => c.BasePrice, new { id = "txtBasePrice", @class = "form-control" })

                    </div>
                    <div class="form-group">
                        <label class="control-label" for="txtName">Selling Price</label>

                        @Html.TextBoxFor(c => c.SellingPrice, new { id = "txtSellingPrice", @class = "form-control" })

                    </div>
                    <div class="form-group">
                        <label class="control-label" for="txtName">Quantity</label>

                        @Html.TextBoxFor(c => c.Quantity, new { id = "txtQuantity", @class = "form-control" })

                    </div>
                    <div class="form-group">
                        <label class="control-label" for="txtName">Discount</label>

                        @Html.TextBoxFor(c => c.Discount, new { id = "txtDiscount", @class = "form-control" })

                    </div>
                    <div class="form-group">
                        <label class="control-label" for="txtName">Sales Tax (%)</label>

                        @Html.TextBoxFor(c => c.SalesTax, new { id = "txtSalesTax", @class = "form-control" })

                    </div>
                    <div class="form-group">
                        <label class="control-label" for="txtName">Active</label>

                        @Html.CheckBoxFor(c => c.Active, new { id = "chkActive", @class = "form-control" })

                    </div>
                    <div class="form-group">
                        <label class="control-label" for="txtName">Image</label>

                        @Html.TextBoxFor(c => c.Image, new { id = "txtImage", @class = "form-control", type = "file" })

                    </div>

                    <div class="form-group">
                        <div class="pull-right col-lg-offset-2">
                            <button id="btnSave" type="submit" class="btn btn-labeled btn-success">
                                <span class="btn-label"><i class="icon-check icon-white"></i></span>Save</button>

                            <button type="reset" class="btn btn-labeled btn-danger">
                                <span class="btn-label"><i class="icon-remove icon-white"></i></span>Cancel</button>
                        </div>
                    </div>
                </div>


            </div>
        </div>
    }


</div>

<script type="text/javascript">


    $(document).ready(function () {


        //------------------------------------------------

        if ($("#txtProductOID").val() == "") {
            $("#title").text('Add Product');
        }
        else {
            $("#title").text('Edit Product');
        }

        //-----------------------------------------------
        var errors = "";

        $("#btnSave").click(function () {

            errors = "";
            if ($("#txtProductCode").val() == "") {
                errors = "Please enter product code.";
            }

            if ($("#txtProductName").val() == "") {
                if (errors != "")
                    errors = errors + "<br/>";

                errors = errors + "Please enter product name.";
            }

            if ($("#txtBasePrice").val() == "") {
                if (errors != "")
                    errors = errors + "<br/>";

                errors = errors + "Please enter base price.";
            }

            if ($("#txtSellingPrice").val() == "") {
                if (errors != "")
                    errors = errors + "<br/>";

                errors = errors + "Please enter selling price.";
            }
            if ($("#txtQuantity").val() == "") {
                if (errors != "")
                    errors = errors + "<br/>";

                errors = errors + "Please enter quantity.";
            }

            if (errors != "") {
                create("themeroller", { title: "Error!", text: errors }, {
                    custom: true,
                    expires: false
                });
                event.preventDefault();
            }

            if ($("#txtSupplierOID").val() == "") {
                return checkExistingProduct();
            }

            return true;
        });
        //------------------------------------------------
        function checkExistingProduct() {
            var value = 0;
            $.ajax({
                type: "POST",
                async: false,
                url: '/Inventory/CheckSupplier',
                data: { supplierName: $("#txtSupplierName").val() },
                dataType: "json",
                processdata: true,
                success: function (result) {
                    var resultMsg = result.Message;
                    if (result.Success == true) {
                        create("themeroller", { title: 'Error!', text: result.Message }, {
                            custom: true,
                            expires: false
                        });
                        value = 1;
                    }
                },
                error: function (e) {

                }
            });

            if (value == 1)
                event.preventDefault();
        }
        //------------------------------------------------
    });// ready
</script>
